using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BaseballGame.Models;

namespace BaseballGame.Controllers
{

	public class GameEngine
	{
		public int ScoreHome = 0;
		public int ScoreAway = 0;
		private int Outs = 0;
		private int batterHome = 1;
		private int batterAway = 1;
		public int HitsHome = 0;
		public int HitsAway = 0;
		private bool b1 = false;
		private bool b2 = false;
		private bool b3 = false;
		private Player at1B;
		private Player at2B;
		private Player at3B;
		private Random random;

		public GameStats currentGame;

		public GameEngine(int HomeID, int AwayID, Random random2)
		{
			random = random2;
			Team Home;
			Team Away;
			int Inning = 1;
			int gameLength = 9;

			currentGame = new GameStats();
			PlayerHittingGameStats batters;
			PlayerPitchingGameStat pitchers;

			game_entitiesDbContext db = new game_entitiesDbContext();

			Home = db.teams.Find(HomeID);
			Home.Catcher = db.players.Find(Home.CatcherId);
			Home.CenterField = db.players.Find(Home.CenterFieldId);
			Home.FirstBase = db.players.Find(Home.FirstBaseId);
			Home.LeftField = db.players.Find(Home.LeftFieldId);
			Home.Rightfield = db.players.Find(Home.RightfieldId);
			Home.SecondBase = db.players.Find(Home.SecondBaseId);
			Home.ShortStop = db.players.Find(Home.ShortStopId);
			Home.ThirdBase = db.players.Find(Home.ThirdBaseId);
			Home.Pitcher = db.players.Find(Home.PitcherId);

			Away = db.teams.Find(AwayID);
			Away.Catcher = db.players.Find(Away.CatcherId);
			Away.CenterField = db.players.Find(Away.CenterFieldId);
			Away.FirstBase = db.players.Find(Away.FirstBaseId);
			Away.LeftField = db.players.Find(Away.LeftFieldId);
			Away.Rightfield = db.players.Find(Away.RightfieldId);
			Away.SecondBase = db.players.Find(Away.SecondBaseId);
			Away.ShortStop = db.players.Find(Away.ShortStopId);
			Away.ThirdBase = db.players.Find(Away.ThirdBaseId);
			Away.Pitcher = db.players.Find(Away.PitcherId);

			currentGame.HomeTeamID = HomeID;
			currentGame.AwayTeamID = AwayID;
			currentGame.HomeBatter = new List<PlayerHittingGameStats>();
			currentGame.AwayBatter = new List<PlayerHittingGameStats>();
			currentGame.HomePitcher = new List<PlayerPitchingGameStat>();
			currentGame.AwayPitcher = new List<PlayerPitchingGameStat>();

			batters = iniPlayer(Home.CatcherId, "Catcher");
			currentGame.HomeBatter.Add(batters);
			batters = iniPlayer(Home.CenterFieldId, "Center Field");
			currentGame.HomeBatter.Add(batters);
			batters = iniPlayer(Home.FirstBaseId, "First Base");
			currentGame.HomeBatter.Add(batters);
			batters = iniPlayer(Home.LeftFieldId, "Left Field");
			currentGame.HomeBatter.Add(batters);
			batters = iniPlayer(Home.RightfieldId, "Right Field");
			currentGame.HomeBatter.Add(batters);
			batters = iniPlayer(Home.SecondBaseId, "Second Base");
			currentGame.HomeBatter.Add(batters);
			batters = iniPlayer(Home.ShortStopId, "Short Stop");
			currentGame.HomeBatter.Add(batters);
			batters = iniPlayer(Home.ThirdBaseId, "Third Base");
			currentGame.HomeBatter.Add(batters);
			batters = iniPlayer(Home.PitcherId, "Pitcher");
			currentGame.HomeBatter.Add(batters);

			batters = iniPlayer(Away.CatcherId, "Catcher");
			currentGame.AwayBatter.Add(batters);
			batters = iniPlayer(Away.CenterFieldId, "Center Field");
			currentGame.AwayBatter.Add(batters);
			batters = iniPlayer(Away.FirstBaseId, "First Base");
			currentGame.AwayBatter.Add(batters);
			batters = iniPlayer(Away.LeftFieldId, "Left Field");
			currentGame.AwayBatter.Add(batters);
			batters = iniPlayer(Away.RightfieldId, "Right Field");
			currentGame.AwayBatter.Add(batters);
			batters = iniPlayer(Away.SecondBaseId, "Second Base");
			currentGame.AwayBatter.Add(batters);
			batters = iniPlayer(Away.ShortStopId, "Short Stop");
			currentGame.AwayBatter.Add(batters);
			batters = iniPlayer(Away.ThirdBaseId, "Third Base");
			currentGame.AwayBatter.Add(batters);
			batters = iniPlayer(Away.PitcherId, "Pitcher");
			currentGame.AwayBatter.Add(batters);

			pitchers = iniPitcher(Home.PitcherId);
			currentGame.HomePitcher.Add(pitchers);
			pitchers = iniPitcher(Away.PitcherId);
			currentGame.AwayPitcher.Add(pitchers);

			while (Inning <= gameLength)
			{
				currentGame.innings++;
				Bat(Away, Home, false, currentGame);
				Outs = 0;

				Bat(Home, Away, true, currentGame);

				if (gameLength == Inning && ScoreHome > ScoreAway)
					break;

				Outs = 0;

				Inning++;

				if (gameLength <= Inning)
				{
					if (ScoreAway == ScoreHome)
						gameLength++;
				}
			}

			//save game to database
			db.gameStats.Add(currentGame);

			db.SaveChanges();


			foreach (PlayerHittingGameStats Player in currentGame.AwayBatter)
			{
				Player.GameId = currentGame.id;
			}

			foreach (PlayerHittingGameStats Player in currentGame.HomeBatter)
			{
				Player.GameId = currentGame.id;
			}

			foreach (PlayerPitchingGameStat Player in currentGame.AwayPitcher)
			{
				Player.GameId = currentGame.id;
			}

			foreach (PlayerPitchingGameStat Player in currentGame.HomePitcher)
			{
				Player.GameId = currentGame.id;
			}

			db.SaveChanges();

			LeagueStandings LeagueStand = db.leagueStandings.FirstOrDefault(t => t.teamId == Home.Id);
			league lige = db.leagues.FirstOrDefault(t => t.team1ID == Home.Id);
			if (lige == null)
			{
				lige = db.leagues.FirstOrDefault(t => t.team2ID == Home.Id);
				if (lige == null)
				{
					lige = db.leagues.FirstOrDefault(t => t.team3ID == Home.Id);
					if (lige == null)
					{
						lige = db.leagues.FirstOrDefault(t => t.team4ID == Home.Id);

					}
				}
			}

			if (LeagueStand == null)
			{
				LeagueStand = new LeagueStandings();
				LeagueStand.teamId = Home.Id;
				LeagueStand.leagueId = lige.LeagueId;
				LeagueStand.games_played = 0;
				LeagueStand.games_Won = 0;
				LeagueStand.games_lost = 0;
				LeagueStand.league_Position = 0;
				LeagueStand.team = Home;
				db.leagueStandings.Add(LeagueStand);
				db.SaveChanges();
			}
			//else
			//db.leagueStandings.Add(LeagueStand);

			LeagueStand.games_played++;
			if (currentGame.HomeScore > currentGame.AwayScore)
			{
				LeagueStand.games_Won++;
			}
			else
			{
				LeagueStand.games_lost++;
			}

			db.SaveChanges();

			//away team
			LeagueStandings LeagueStand2 = db.leagueStandings.FirstOrDefault(t => t.teamId == Away.Id);
			league lige2 = db.leagues.FirstOrDefault(t => t.team1ID == Away.Id);
			if (lige2 == null)
			{
				lige2 = db.leagues.FirstOrDefault(t => t.team2ID == Away.Id);
				if (lige2 == null)
				{
					lige2 = db.leagues.FirstOrDefault(t => t.team3ID == Away.Id);
					if (lige2 == null)
					{
						lige2 = db.leagues.FirstOrDefault(t => t.team4ID == Away.Id);

					}
				}
			}

			if (LeagueStand2 == null)
			{
				LeagueStand2 = new LeagueStandings();
				LeagueStand2.teamId = Home.Id;
				LeagueStand2.leagueId = lige.LeagueId;
				LeagueStand2.games_played = 0;
				LeagueStand2.games_Won = 0;
				LeagueStand2.games_lost = 0;
				LeagueStand2.league_Position = 0;
				LeagueStand2.team = Away;
				db.leagueStandings.Add(LeagueStand2);
				db.SaveChanges();
			}
			//else
			//db.leagueStandings.Add(LeagueStand2);

			LeagueStand2.games_played++;
			if (currentGame.AwayScore > currentGame.HomeScore)
			{
				LeagueStand2.games_Won++;
			}
			else
			{
				LeagueStand2.games_lost++;
			}

			db.SaveChanges();
		}

		void initStats()
		{
			currentGame.awayHits = 0;
			currentGame.awayHR = 0;
			currentGame.AwayScore = 0;
			currentGame.botIn1 = 0;
			currentGame.botIn2 = 0;
			currentGame.botIn3 = 0;
			currentGame.botIn4 = 0;
			currentGame.botIn5 = 0;
			currentGame.botIn6 = 0;
			currentGame.botIn7 = 0;
			currentGame.botIn8 = 0;
			currentGame.botIn9 = 0;
			currentGame.botInE = 0;
			currentGame.homeHits = 0;
			currentGame.homeHR = 0;
			currentGame.HomeScore = 0;
			currentGame.innings = 0;
			currentGame.topIn1 = 0;
			currentGame.topIn2 = 0;
			currentGame.topIn3 = 0;
			currentGame.topIn4 = 0;
			currentGame.topIn5 = 0;
			currentGame.topIn6 = 0;
			currentGame.topIn7 = 0;
			currentGame.topIn8 = 0;
			currentGame.topIn9 = 0;
			currentGame.topInE = 0;
		}



		int hit(int bases, Player currentBatter)
		{
			int score = 0;
			int paBases = PlayersAtBases();

			switch (bases)
			{
				case 1: //single
					{
						if (paBases == 1)// Case 1: 1B
						{
							at2B = at1B;
							at1B = currentBatter;
							b2 = true;
						}

						else if (paBases == 2)// Case 2: 1B, 2B
						{
							at3B = at2B;
							at2B = at1B;
							at1B = currentBatter;
							b3 = true;
						}
						else if (paBases == 3)// Case 3: 1B, 3B
						{
							at2B = at1B;
							at1B = currentBatter;
							b2 = true;
						}
						else if (paBases == 4)// Case 4: 1B, 2B, 3B
						{
							at3B = at2B;
							at2B = at1B;
							at1B = currentBatter;
							score++;
						}
						else//case 5 6 7 nobody on 1st
						{
							at1B = currentBatter;
							b1 = true;
						}
						break;
					}
				case 2: //double
					{
						if (paBases == 1)// Case 1: 1B
						{
							at3B = at1B;
							at2B = currentBatter;
							at1B = null;
							b1 = false;
							b2 = true;
							b3 = true;
						}
						else if (paBases == 2)// Case 2: 1B, 2B
						{
							at3B = at1B;
							at2B = currentBatter;
							at1B = null;
							score++;
							b1 = false;
							b2 = true;
							b3 = true;
						}
						else if (paBases == 3)// Case 3: 1B, 3B
						{
							at3B = at1B;
							at2B = currentBatter;
							at1B = null;
							score++;
							b1 = false;
							b2 = true;
							b3 = true;
						}
						else if (paBases == 4)// Case 4: 1B, 2B, 3B
						{
							at3B = at1B;
							at2B = currentBatter;
							at1B = null;
							score++;
							score++;
							b1 = false;
						}
						else if (bases == 5)// Case 5: 2B
						{
							at3B = at2B;
							at2B = currentBatter;
							at1B = null;
							b1 = false;
							b2 = true;
							b3 = true;
						}
						else if (bases == 6)// Case 6: 2B 3B
						{
							at3B = at2B;
							at2B = currentBatter;
							at1B = null;
							b1 = false;
							b2 = true;
							b3 = true;
							score++;
						}
						else //0 or 7
						{
							at1B = null;
							at2B = currentBatter;
							b2 = true;
						}
						break;
					}
				case 3: //triple
					{
						at3B = currentBatter;
						at2B = null;
						at1B = null;
						if (b1)
							score++;
						if (b2)
							score++;
						if (b3)
							score++;
						b3 = true;
						b1 = false;
						b2 = false;
						break;
					}
				case 4: //HR
					{
						at3B = null;
						at2B = null;
						at1B = null;
						if (b1)
							score++;
						if (b2)
							score++;
						if (b3)
							score++;
						score++;
						b1 = false;
						b2 = false;
						b3 = false;


						break;
					}
				default:
					{
						break;
					}
			}
			return score;
		}

		PlayerPitchingGameStat iniPitcher(int playerID)
		{
			PlayerPitchingGameStat Pitcher = new PlayerPitchingGameStat();

			Pitcher.bb = 0;
			Pitcher.er = 0;
			Pitcher.h = 0;
			Pitcher.hr = 0;
			Pitcher.ip = 0;
			Pitcher.k = 0;
			Pitcher.r = 0;
			Pitcher.PLayerId = playerID;

			return Pitcher;
		}

		PlayerHittingGameStats iniPlayer(int playerID, string Position)
		{
			PlayerHittingGameStats batters = new PlayerHittingGameStats();

			batters.PLayerId = playerID;
			batters.atBat = 0;
			batters.bb = 0;
			batters.hits = 0;
			batters.hr = 0;
			batters.k = 0;
			batters.lob = 0;
			batters.rbi = 0;
			batters.runs = 0;
			batters.sb = 0;
			batters.position = Position;

			return batters;
		}
		/// <summary>
		/// jnbjn
		/// </summary>
		/// <param name="A"></param>
		/// <param name="B"></param>
		/// <param name="random"></param>
		/// <param name="Home"></param>
		void Bat(Team A, Team B, bool Home, GameStats currentGame)
		{
			int plate = 0;
			int BattingTeam = 0;
			int hits = 0;
			int batter = 1;
			if (Home)
			{
				batter = batterHome;
			}
			else
			{
				batter = batterAway;
			}

			b1 = false;
			b2 = false;
			b3 = false;



			while (Outs < 3)
			{

				switch (batter)
				{
					case 1:
						plate = HitOrOut(A.FirstBase, B.Pitcher);
						GeneratePlayerStats(A.FirstBase, Home, "First Base", plate, B.Pitcher);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam += hit(plate, A.FirstBase);
						}
						break;
					case 2:
						plate = HitOrOut(A.SecondBase, B.Pitcher);
						GeneratePlayerStats(A.SecondBase, Home, "Second Base", plate, B.Pitcher);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam += hit(plate, A.SecondBase);
						}
						break;
					case 3:
						plate = HitOrOut(A.ThirdBase, B.Pitcher);
						GeneratePlayerStats(A.ThirdBase, Home, "Third Base", plate, B.Pitcher);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam += hit(plate, A.ThirdBase);
						}
						break;
					case 4:
						plate = HitOrOut(A.ShortStop, B.Pitcher);
						GeneratePlayerStats(A.ShortStop, Home, "Short Stop", plate, B.Pitcher);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam += hit(plate, A.ShortStop);
						}
						break;
					case 5:
						plate = HitOrOut(A.Catcher, B.Pitcher);
						GeneratePlayerStats(A.Catcher, Home, "Catcher", plate, B.Pitcher);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam += hit(plate, A.Catcher);
						}
						break;
					case 6:
						plate = HitOrOut(A.LeftField, B.Pitcher);
						GeneratePlayerStats(A.LeftField, Home, "Left Field", plate, B.Pitcher);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam += hit(plate, A.LeftField);
						}
						break;
					case 7:
						plate = HitOrOut(A.Rightfield, B.Pitcher);
						GeneratePlayerStats(A.Rightfield, Home, "Right Field", plate, B.Pitcher);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam += hit(plate, A.Rightfield);
						}
						break;
					case 8:
						plate = HitOrOut(A.CenterField, B.Pitcher);
						GeneratePlayerStats(A.CenterField, Home, "Center Field", plate, B.Pitcher);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam += hit(plate, A.CenterField);
						}
						break;
					case 9:
						plate = HitOrOut(A.Pitcher, B.Pitcher);
						GeneratePlayerStats(A.Pitcher, Home, "Pitcher", plate, B.Pitcher);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam += hit(plate, A.Pitcher);
						}
						break;
					default:

						break;
				}
				batter++;
				if (batter > 9)
					batter = 1;
				if (Home && plate == 4)
					currentGame.homeHR++;
				else if (!Home && plate == 4)
					currentGame.awayHR++;
			}

			if (Home)
			{
				ScoreHome += BattingTeam;
				currentGame.HomeScore = ScoreHome;
				batterHome = batter;
				HitsHome += hits;
				currentGame.homeHits = HitsHome;
				updateStats(true, BattingTeam);
			}
			else
			{
				ScoreAway += BattingTeam;
				currentGame.AwayScore = ScoreAway;
				batterAway = batter;
				HitsAway += hits;
				currentGame.awayHits = HitsAway;
				updateStats(false, BattingTeam);
			}

			//take care of people left on base
			if (b1)
			{
				PlayerHittingGameStats temp = position(at1B.Id, Home);
				temp.lob++;
				at1B.left_on_base++;
			}
			if (b2)
			{
				PlayerHittingGameStats temp = position(at2B.Id, Home);
				temp.lob++;
				at2B.left_on_base++;
			}
			if (b3)
			{
				PlayerHittingGameStats temp = position(at3B.Id, Home);
				temp.lob++;
				at3B.left_on_base++;
			}
			b1 = false;
			b2 = false;
			b3 = false;
			at1B = null;
			at2B = null;
			at3B = null;
		}

		void updateStats(Boolean home, int runs)
		{
			switch (currentGame.innings)
			{
				case 1:
					{
						if (home)
						{
							currentGame.botIn1 += runs;
						}
						else
						{
							currentGame.topIn1 += runs;
						}
						break;
					}
				case 2:
					{
						if (home)
						{
							currentGame.botIn2 += runs;
						}
						else
						{
							currentGame.topIn2 += runs;
						}
						break;
					}
				case 3:
					{
						if (home)
						{
							currentGame.botIn3 += runs;
						}
						else
						{
							currentGame.topIn3 += runs;
						}
						break;
					}
				case 4:
					{
						if (home)
						{
							currentGame.botIn4 += runs;
						}
						else
						{
							currentGame.topIn4 += runs;
						}
						break;
					}
				case 5:
					{
						if (home)
						{
							currentGame.botIn5 += runs;
						}
						else
						{
							currentGame.topIn5 += runs;
						}
						break;
					}
				case 6:
					{
						if (home)
						{
							currentGame.botIn6 += runs;
						}
						else
						{
							currentGame.topIn6 += runs;
						}
						break;
					}
				case 7:
					{
						if (home)
						{
							currentGame.botIn7 += runs;
						}
						else
						{
							currentGame.topIn7 += runs;
						}
						break;
					}
				case 8:
					{
						if (home)
						{
							currentGame.botIn8 += runs;
						}
						else
						{
							currentGame.topIn8 += runs;
						}
						break;
					}
				case 9:
					{
						if (home)
						{
							currentGame.botIn9 += runs;
						}
						else
						{
							currentGame.topIn9 += runs;
						}
						break;
					}
				default:
					{
						if (home)
						{
							currentGame.botInE += runs;
						}
						else
						{
							currentGame.topInE += runs;
						}
						break;
					}
			}
		}
		/// <summary>
		/// Return
		/// 0: Out,
		/// 1: Single,
		/// 2: Double,
		/// 3: Thriple,
		/// 4: HR,
		/// </summary>
		/// <param name="Batter"></param>
		/// <param name="Pitcher"></param>
		/// <returns></returns>
		int HitOrOut(Player Batter, Player Pitcher)
		{

			int randomNumber = random.Next(0, 100);
			int HittingChance = Batter.hitting_chance;
			int PitchingChance = Pitcher.pitching;
			int TotalChance = HittingChance - PitchingChance + 25;

			if (randomNumber > TotalChance)
			{
				return 0;
			}
			else
			{
				randomNumber = random.Next(0, 100);
				if (randomNumber < Batter.power * .15)
				{
					return 4;
				}
				else if (randomNumber < Batter.power * .30)
				{
					return 3;
				}
				else if (randomNumber < Batter.power)
				{
					return 2;
				}
				else
				{
					return 1;
				}
			}
		}

		/// <summary>
		/// Case 0: empty
		/// Case 1: 1B
		/// Case 2: 1B, 2B
		/// Case 3: 1B, 3B
		/// Case 4: 1B, 2B, 3B
		/// Case 5: 2B
		/// Case 6: 2B 3B
		/// Case 7: 3B
		/// </summary>
		/// <returns></returns>
		private int PlayersAtBases()
		{
			if (b1 && b2 && b3)
				return 4;
			else if (b1 && b2)
				return 2;
			else if (b1 && b3)
				return 3;
			else if (b1)
				return 1;
			else if (b2 && b3)
				return 6;
			else if (b2)
				return 5;
			else if (b3)
				return 7;
			else
				return 0;

		}

		private PlayerHittingGameStats position(int id, bool home)
		{
			if (home)
			{
				foreach (PlayerHittingGameStats person in currentGame.HomeBatter)
				{
					if (person.PLayerId == id)
						return person;
				}
			}
			else
			{
				foreach (PlayerHittingGameStats person in currentGame.AwayBatter)
				{
					if (person.PLayerId == id)
						return person;
				}
			}

			return null;
		}

		private void strikeOut(Player Batter, PlayerHittingGameStats BatterStats, Player Pitcher, bool home)
		{
			int strikeOutChance = Pitcher.pitching - Batter.hitting_chance;
			int randomNumber = random.Next(0, 100);
			if (randomNumber < strikeOutChance)//k
			{
				BatterStats.k++;
				Pitcher.strike_outs++;
				HomeOrAwayPitcher(home).k++;
			}
		}

		private PlayerPitchingGameStat HomeOrAwayPitcher(bool home)
		{
			if (!home)
				return currentGame.HomePitcher[currentGame.HomePitcher.Count() - 1];
			else
				return currentGame.AwayPitcher[currentGame.AwayPitcher.Count() - 1];
		}

		private void AddOneRunAllowed(Player Pitcher, PlayerPitchingGameStat PitcherStat)
		{
			Pitcher.runs_allowed++;
			Pitcher.earned_runs_allowed++;
			PitcherStat.r++;
			PitcherStat.er++;
		}

		private void GenerateStats(Player Batter, PlayerHittingGameStats BatterStats, int plate, bool home, Player Pitcher)
		{
			int bases = PlayersAtBases();
			Batter.at_bat++;
			BatterStats.atBat++;
			PlayerHittingGameStats temp;
			switch (plate)
			{
				case 0://out
					{
						//Call function that gives a strikeout or normal out
						strikeOut(Batter, BatterStats, Pitcher, home);
						AddIP(Pitcher, HomeOrAwayPitcher(home));
						break;
					}
				case 1://single
					{
						Batter.hits++;
						BatterStats.hits++;
						HomeOrAwayPitcher(home).h++;
						if (b3)
						{
							Batter.runs_batted_in++;
							BatterStats.rbi++;
							at3B.runs++;
							temp = position(at3B.Id, home);
							temp.runs++;
							AddOneRunAllowed(Pitcher, HomeOrAwayPitcher(home));
						}
						break;
					}
				case 2://double
					{
						Batter.hits++;
						BatterStats.hits++;
						HomeOrAwayPitcher(home).h++;
						if (bases == 1)//person on first 
						{
							//no stat changes
						}
						else if (bases == 2)//person on first and second
						{
							//second scores
							Batter.runs_batted_in++;
							BatterStats.rbi++;
							at2B.runs++;
							temp = position(at2B.Id, home);
							temp.runs++;
							AddOneRunAllowed(Pitcher, HomeOrAwayPitcher(home));
						}
						else if (bases == 3)//person on first and third
						{
							//3rd scores
							Batter.runs_batted_in++;
							BatterStats.rbi++;
							at3B.runs++;
							temp = position(at3B.Id, home);
							temp.runs++;
							AddOneRunAllowed(Pitcher, HomeOrAwayPitcher(home));
						}
						else if (bases == 4) //bases loaded
						{
							//second and 3rd score
							Batter.runs_batted_in++;
							BatterStats.rbi++;
							at2B.runs++;
							temp = position(at2B.Id, home);
							temp.runs++;
							AddOneRunAllowed(Pitcher, HomeOrAwayPitcher(home));

							Batter.runs_batted_in++;
							BatterStats.rbi++;
							at3B.runs++;
							temp = position(at3B.Id, home);
							temp.runs++;
							AddOneRunAllowed(Pitcher, HomeOrAwayPitcher(home));
						}
						else if (bases == 5)//person on second
						{
							//no stat changes
						}
						else if (bases == 6)//person and second and third
						{
							//third scores
							Batter.runs_batted_in++;
							BatterStats.rbi++;
							at3B.runs++;
							temp = position(at3B.Id, home);
							temp.runs++;
							AddOneRunAllowed(Pitcher, HomeOrAwayPitcher(home));
						}
						else//person on third
						{
							//no stat changes
						}
						break;
					}
				case 3://thriple
					{
						Batter.hits++;
						BatterStats.hits++;
						HomeOrAwayPitcher(home).h++;

						if (b1)
						{
							Batter.runs_batted_in++;
							BatterStats.rbi++;
							at1B.runs++;
							temp = position(at1B.Id, home);
							temp.runs++;
							AddOneRunAllowed(Pitcher, HomeOrAwayPitcher(home));
						}
						if (b2)
						{
							Batter.runs_batted_in++;
							BatterStats.rbi++;
							at2B.runs++;
							temp = position(at2B.Id, home);
							temp.runs++;
							AddOneRunAllowed(Pitcher, HomeOrAwayPitcher(home));
						}
						if (b3)
						{
							Batter.runs_batted_in++;
							BatterStats.rbi++;
							at3B.runs++;
							temp = position(at3B.Id, home);
							temp.runs++;
							AddOneRunAllowed(Pitcher, HomeOrAwayPitcher(home));
						}
						break;
					}
				case 4: //hr
					{
						Batter.hits++;
						BatterStats.hits++;
						Batter.home_runs++;
						BatterStats.hr++;
						Batter.runs++;
						BatterStats.runs++;
						HomeOrAwayPitcher(home).h++;
						HomeOrAwayPitcher(home).hr++;
						AddOneRunAllowed(Pitcher, HomeOrAwayPitcher(home));
						Pitcher.home_runs_allowed++;
						if (b1)
						{
							Batter.runs_batted_in++;
							BatterStats.rbi++;
							at1B.runs++;
							temp = position(at1B.Id, home);
							temp.runs++;
							AddOneRunAllowed(Pitcher, HomeOrAwayPitcher(home));
						}
						if (b2)
						{
							Batter.runs_batted_in++;
							BatterStats.rbi++;
							at2B.runs++;
							temp = position(at2B.Id, home);
							temp.runs++;
							AddOneRunAllowed(Pitcher, HomeOrAwayPitcher(home));
						}
						if (b3)
						{
							Batter.runs_batted_in++;
							BatterStats.rbi++;
							at3B.runs++;
							temp = position(at3B.Id, home);
							temp.runs++;
							AddOneRunAllowed(Pitcher, HomeOrAwayPitcher(home));
						}
						break;
					}
			}
			double era = Math.Round((Pitcher.runs_allowed / Pitcher.innings_pitched), 2);
			double avg = Math.Round(((double)Batter.hits / (double)Batter.at_bat), 3);
			Batter.batting_avg = avg;
			Pitcher.earned_runs_average = era;
		}

		private void AddIP(Player Pitcher, PlayerPitchingGameStat PitcherStats)
		{
			double CurrentIP = Math.Round(Pitcher.innings_pitched, 1);
			int CurrentIPStats = PitcherStats.ip;

			if ((CurrentIP * 10) % 10 == 6)
				CurrentIP += 0.4;
			else
				CurrentIP += 0.3;

			if (CurrentIPStats % 10 == 6)
				CurrentIPStats += 4;
			else
				CurrentIPStats += 3;

			Pitcher.innings_pitched = CurrentIP;
			PitcherStats.ip = CurrentIPStats;
		}

		private void GeneratePlayerStats(Player BatterObject, Boolean Home, string position, int plate, Player PitcherObject)
		{
			if (Home)
			{
				foreach (PlayerHittingGameStats person in currentGame.HomeBatter)
				{
					if (position == person.position)
					{
						GenerateStats(BatterObject, person, plate, Home, PitcherObject);
					}
				}
			}
			else
			{
				foreach (PlayerHittingGameStats person in currentGame.AwayBatter)
				{
					if (position == person.position)
					{
						GenerateStats(BatterObject, person, plate, Home, PitcherObject);
					}
				}
			}
		}

	}
}